package com.basemanage.controller.server;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.basemanage.core.controller.JCBaseController;
import com.basemanage.core.model.Condition;
import com.basemanage.core.model.Operators;
import com.basemanage.core.util.CommonUtils;
import com.basemanage.core.util.JqGridModelUtils;
import com.basemanage.core.view.InvokeResult;
import com.basemanage.model.SmServerproject;
import com.basemanage.model.SmServertype;
import com.basemanage.service.ServerTypeService;
import com.basemanage.service.ServerprojectService;
import com.basemanage.service.StoreService;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.xiaoleilu.hutool.json.JSONObject;

public class ServerprojectController extends JCBaseController {
	
	public void index() {
		render("serverproject_index.html");
	}
	
	/**
	 * 跳转（添加/编辑）页面
	 */
	public void addServerproject(){
		Integer id=getParaToInt("id");
		String action="add";
		if(id!=null){
			this.setAttr("item", SmServerproject.dao.findById(id));
			action="edit";
		}
		this.setAttr("action", action);
		//需要显示分类
		this.setAttr("types", ServerTypeService.me.getTypelist());
		this.setAttr("stores", StoreService.me.getStorelist());
		render("serverproject_add.html");
	}
	
	
	/**
	 * 数据分页列表
	 */
	public void getListData() {
		
		String name=this.getPara("name");
		Set<Condition> conditions=new HashSet<Condition>();
		if(CommonUtils.isNotEmpty(name)){
			conditions.add(new Condition("remark",Operators.LIKE,name));
		}
		
//		Page<SmServerproject> pageInfo=SmServerproject.dao.getPage(this.getPage(), this.getRows(),conditions,this.getOrderby());

		List<Record> records = ServerTypeService.me.getTypeDetail();
//		public Page(List<T> list, int pageNumber, int pageSize, int totalPage, int totalRow) 
		Page<Record> pageInfo = new Page<Record>(records,this.getPage(),this.getRows(),1,2);
		this.renderJson(JqGridModelUtils.toJqGridView(pageInfo)); 
	}
	
	/**
	 * 保存数据
	 */
	public void saveServerproject(){
		SmServerproject serverproject=getModel(SmServerproject.class,"obj");
		InvokeResult result=ServerprojectService.me.saveServerproject(serverproject);
		this.renderJson(result);
	}
	 
	/**
	 * 删除数据
	 */
	public void delServerproject(){
		String ids=this.getPara("ids");
		InvokeResult result=ServerprojectService.me.delServerproject(ids);
		this.renderJson(result);
	}
	/*
	 * 上传图片
	 */
	public void uploadimg() {

	    UploadFile uploadFile = getFile();//在磁盘上保存文件

	    JSONObject data = new JSONObject();

	    StringBuffer URL= getRequest().getRequestURL();
	    String prefixString = URL.substring(0, 21);

        if (uploadFile != null) {
            String fileName = uploadFile.getFileName();
            String extentionName = fileName.substring(fileName
                    .lastIndexOf(".")); // 后缀名
         
            String newName =  prefixString + "/uploadfile/" + fileName;// 新名
//            String filePath = PathKit.getWebRootPath() + PropKit.get("uploadPath") + "/" + newName;// 文件完整路径
            uploadFile.getFile().renameTo(new File(newName)); // 重命名并上传文件

            //返回任意数据即代表上传成功
            data.put("filePath", newName);
            data.put("status", "success");
            renderJson(InvokeResult.success(data));
        }else {
            //返回任意数据即代表上传成功
        	data.put("error", "未选择文件");
        	data.put("status", "error");
            renderJson(InvokeResult.success(data));
        }

		
	}
	
	/*
	 * 获取类型名称
	 */
	public void getTypeName(){
		
		
		Integer typeid = getParaToInt("serverid");
		InvokeResult result = ServerprojectService.me.getType(typeid);
		this.renderJson(result);
		
	}
	
}